//
//  SLNewMineViewController.m
//  StarLight
//
//  Created by apple on 16/7/30.
//  Copyright © 2016年 Duke.Wu. All rights reserved.
//

#import "SLNewMineViewController.h"
#import "SLShipOwnerVipLevelView.h"
#import "SLUserManager.h"
#import "SLMineViewModel.h"
#import "SLMoreSetViewController.h"
#import "SLMessageCenterViewController.h"
#import "SLBackListViewController.h"
#import "SLVIPJoinViewController.h"
#import "SLChangeHeadeViewController.h"
#import "SLNewViPCollectionViewCell.h"
#import "SLNewAttentionCollectionViewCell.h"
#import "SLFirmApproveViewController.h"

@interface SLNewMineViewController ()<UITableViewDelegate, UITableViewDataSource,UICollectionViewDelegate,UICollectionViewDataSource,UICollectionViewDelegateFlowLayout>

@property (nonatomic ,strong) UITableView *choicesTableView;

@property (nonatomic ,strong) UICollectionView *headCollectionView;

@property (nonatomic, strong) SLMineViewModel *mineViewModel;

@property (nonatomic, strong) NSMutableArray *dataSource;

@property (nonatomic ,strong) NSMutableArray *titleArr;

@property (nonatomic ,strong) NSMutableArray *AttentionNumArr;

@property (nonatomic ,strong) NSString *photoUrl;


@end

@implementation SLNewMineViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self setUpMineUI];
    [self addConstrains];
    [self addObservers];
    [self refreshMine];
    // Do any additional setup after loading the view.
}
- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    [self.naviHeader setNaviTitleWithString:@"我的"];
}

- (void)addObservers{
    @weakify(self)
    [RACObserve(self.mineViewModel, indexModel) subscribeNext:^(SLMineUserIndexModel *model) {
        @strongify(self)
        if (model) {
            [self configSelfWithIndexModel:model];
            [self.AttentionNumArr addObject:model.popularity];
            [self.AttentionNumArr addObject:model.gzhnum];
            [self.AttentionNumArr addObject:model.mgznum];
            self.photoUrl = model.headPhoto;
            [self.choicesTableView reloadData];
            [self.headCollectionView reloadData];
        }
        
    }];
    
    // 更换头像后 刷新页面
    [[[NSNotificationCenter defaultCenter] rac_addObserverForName:kChangeHeadSuccess object:nil] subscribeNext:^(NSNotification *notify) {
        @strongify(self)
        [self refreshMine];
    }];

    // 会员付款成功后 刷新页面
    [[[NSNotificationCenter defaultCenter] rac_addObserverForName:kJoinVipSucess object:nil] subscribeNext:^(id x) {
        
        [self refreshMine];
    }];
    
}

- (void)refreshMine{
    [self.mineViewModel getUserindexCallBack:^(id respond, NSError *error) {
        [self configSelfWithIndexModel:self.mineViewModel.indexModel];
    }];
}
static NSInteger userType;

// 数据更新的时候 更新 UI
- (void)configSelfWithIndexModel:(SLMineUserIndexModel *)model{
    
    userType = model.type;
    
}
#pragma mark - addMethod
- (void)inviteReward{

}
- (void)VIPHandle{
    SLVIPJoinViewController *vipVC = [SLVIPJoinViewController new];
    vipVC.userType = userType;
    vipVC.memberImage = self.photoUrl;
    vipVC.hidesBottomBarWhenPushed = YES;
    [self.navigationController pushViewController:vipVC animated:YES];
}
- (void)verified{
    SLFirmApproveViewController *firm = [[SLFirmApproveViewController alloc] init];
    firm.hidesBottomBarWhenPushed = YES;
    [self.navigationController pushViewController:firm animated:YES];
}
- (void)Feedback{
    SLBackListViewController *backListVC = [SLBackListViewController new];
    backListVC.hidesBottomBarWhenPushed = YES;
    [self.navigationController pushViewController:backListVC animated:YES];
}
- (void)systemSeting{
    SLMoreSetViewController *moreSetVC = [[SLMoreSetViewController alloc] init];
    moreSetVC.hidesBottomBarWhenPushed = YES;
    [self.navigationController pushViewController:moreSetVC animated:YES];
}
- (void)switchUser{
    [SLUserManager loginOut];
}

- (void)setPhoto:(NSString *)imageUrl{
    SLChangeHeadeViewController *changeVC = [SLChangeHeadeViewController new];
    changeVC.oldHead = imageUrl;
    changeVC.hidesBottomBarWhenPushed = YES;
    [self.navigationController pushViewController:changeVC animated:YES];
}

#pragma mark - 设置Cell分割线从最左边开始
-(void)viewDidLayoutSubviews
{
    if ([self.choicesTableView respondsToSelector:@selector(setSeparatorInset:)]) {
        [self.choicesTableView setSeparatorInset:UIEdgeInsetsMake(0,0,0,0)];
    }
    
    if ([self.choicesTableView respondsToSelector:@selector(setLayoutMargins:)]) {
        [self.choicesTableView setLayoutMargins:UIEdgeInsetsMake(0,0,0,0)];
    }
}

#pragma mark - UICollectionViewDelegate,UICollectionViewDataSource,UICollectionViewDelegateFlowLayout


- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return 4;
}

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.row == 0) {
        return CGSizeMake(SCREEN_WIDTH, 94);
    }
    else{
        return CGSizeMake(SCREEN_WIDTH / 3.0, 70);
    }
}

- (void)collectionView:(UICollectionView *)collectionView willDisplayCell:(UICollectionViewCell *)cell forItemAtIndexPath:(NSIndexPath *)indexPath
{
    if ([cell isKindOfClass:[SLNewViPCollectionViewCell class]]) {
        SLNewViPCollectionViewCell *displayCell = (SLNewViPCollectionViewCell *)cell;
        [displayCell cellForData:self.mineViewModel.indexModel];
    }
    if ([cell isKindOfClass:[SLNewAttentionCollectionViewCell class]]) {
        SLNewAttentionCollectionViewCell *displayCell = (SLNewAttentionCollectionViewCell *)cell;
        if (indexPath.row > 0) {
            [displayCell cellForTitle:self.titleArr[indexPath.row - 1] num:self.AttentionNumArr[indexPath.row - 1]];
        }
    }
}


- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.row == 0) {
        SLNewViPCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"SLNewViPCollectionViewCell" forIndexPath:indexPath];
        @weakify(self)
        cell.pushSetPhoto = ^(){
            @strongify(self)
            [self setPhoto:self.mineViewModel.indexModel.headPhoto];
        };
        return cell;
    }
    else{
        SLNewAttentionCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"SLNewAttentionCollectionViewCell" forIndexPath:indexPath];
        return cell;
    }
}

#pragma mark - UITableViewDelegate, UITableViewDataSource

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 2;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 3;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 41;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 14;
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
    return 0.1;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.section == 0) {
        switch (indexPath.row) {
            case 0:
                [self inviteReward];
                break;
            case 1:
//                [self VIPHandle];
                break;
            case 2:
                [self verified];
                break;
                
            default:
                break;
        }
    }else{
        switch (indexPath.row) {
            case 0:
                [self Feedback];
                break;
            case 1:
                [self systemSeting];
                break;
            case 2:
                [self switchUser];
                break;
                
            default:
                break;
        }
    }
}

- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath
{
    if ([cell respondsToSelector:@selector(setSeparatorInset:)]) {
        [cell setSeparatorInset:UIEdgeInsetsZero];
    }
    
    if ([cell respondsToSelector:@selector(setLayoutMargins:)]) {
        [cell setLayoutMargins:UIEdgeInsetsZero];
    }
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.section == 0) {
        if (indexPath.row == 2) {
            UITableViewCell *cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:@"cell1"];
            cell.selectionStyle = UITableViewCellSelectionStyleNone;
            cell.imageView.image = [UIImage imageNamed:@"ic_mine_shiming"];
            cell.textLabel.text = @"实名认证";
            cell.textLabel.font = sFont(14);
            cell.detailTextLabel.font = sFont(14);
            cell.detailTextLabel.textColor = RGBCOLOR(51, 51, 51);
            cell.textLabel.textColor = RGBCOLOR(51, 51, 51);
            cell.accessoryType=UITableViewCellAccessoryDisclosureIndicator;
            switch (self.mineViewModel.indexModel.vitality) {
                case 0:
                    cell.detailTextLabel.text = @"未认证";
                    break;
                case 1:
                    cell.detailTextLabel.text = @"审核中";
                    break;
                case 2:
                    cell.detailTextLabel.text = @"认证失败";
                    break;
                case 3:
                    cell.detailTextLabel.text = @"已认证";
                    break;
                    
                default:
                    break;
            }
            return cell;
        }
    }
    
    UITableViewCell *cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:@"cell"];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    cell.textLabel.font = sFont(14);
    cell.detailTextLabel.font = sFont(14);
    cell.detailTextLabel.textColor = RGBCOLOR(51, 51, 51);
    cell.textLabel.textColor = RGBCOLOR(51, 51, 51);
    cell.accessoryType=UITableViewCellAccessoryDisclosureIndicator;
    if (indexPath.section == 0) {
        if (indexPath.row == 0) {
            cell.detailTextLabel.text = @"敬请期待";
        }
        else if (indexPath.row == 1)
        {
            cell.detailTextLabel.text = @"敬请期待";
        }
        cell.imageView.image = [UIImage imageNamed:self.dataSource[0][indexPath.row][@"image"]];
        cell.textLabel.text = self.dataSource[0][indexPath.row][@"text"];
    }else if (indexPath.section == 1){
        cell.imageView.image = [UIImage imageNamed:self.dataSource[1][indexPath.row][@"image"]];
        cell.textLabel.text = self.dataSource[1][indexPath.row][@"text"];
        
    }
    
    return cell;
}

#pragma mark - addUI
- (void)setUpMineUI
{
    self.view.backgroundColor = [UIColor defeutBackgroudColor];
    [self.view addSubview:self.choicesTableView];
    [self.view addSubview:self.headCollectionView];    
}

- (void)addConstrains
{
    
    [self.headCollectionView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.view).with.offset(64);
        make.width.equalTo(self.view);
        make.height.mas_equalTo(164);
        make.centerX.equalTo(self.view);
    }];
    
    [self.choicesTableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.headCollectionView.mas_bottom);
        make.centerX.equalTo(self.view);
        make.bottom.equalTo(self.mas_bottomLayoutGuideBottom);
        make.width.equalTo(self.view);
    }];
    
    
}

#pragma mark - get
- (NSMutableArray *)dataSource{
    if (!_dataSource) {
        _dataSource = [@[
  @[@{@"text":@"邀请奖励",@"image":@"ic_mine_yaoqing"},
  @{@"text":@"增值服务",@"image":@"ic_mine_vip"}],
  @[@{@"text":@"意见反馈",@"image":@"ic_mine_yijian"},
  @{@"text":@"系统设置",@"image":@"ic_mine_set"},
  @{@"text":@"切换用户",@"image":@"ic_mine_qiehuan"}]
  ] mutableCopy];
    }
    return _dataSource;
}
- (NSMutableArray *)titleArr
{
    if (!_titleArr) {
        _titleArr = [@[@"人气",@"关注我的",@"我关注的"] mutableCopy];
    }
    return _titleArr;
}
- (NSMutableArray *)AttentionNumArr
{
    if (!_AttentionNumArr) {
        _AttentionNumArr = [NSMutableArray arrayWithCapacity:1];
    }
    return _AttentionNumArr;
}


- (UITableView *)choicesTableView{
    if (!_choicesTableView) {
        _choicesTableView = [[UITableView alloc] initWithFrame:CGRectZero style:UITableViewStyleGrouped];
        [_choicesTableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"cell"];
        [_choicesTableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"cell1"];
        _choicesTableView.delegate = self;
        _choicesTableView.dataSource = self;
        _choicesTableView.tableFooterView = [UIView new];
    }
    return _choicesTableView;
}


- (UICollectionView *)headCollectionView
{
    if (!_headCollectionView) {
        UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
        layout.minimumLineSpacing = 0;
        layout.minimumInteritemSpacing = 0;
        _headCollectionView = [[UICollectionView alloc] initWithFrame:CGRectZero collectionViewLayout:layout];
        [_headCollectionView registerClass:[SLNewViPCollectionViewCell class] forCellWithReuseIdentifier:@"SLNewViPCollectionViewCell"];
        [_headCollectionView registerClass:[SLNewAttentionCollectionViewCell class] forCellWithReuseIdentifier:@"SLNewAttentionCollectionViewCell"];
        _headCollectionView.backgroundColor =[UIColor whiteColor];
        _headCollectionView.delegate = self;
        _headCollectionView.dataSource = self;
    }
    return _headCollectionView;
}
- (SLMineViewModel *)mineViewModel{
    if (!_mineViewModel) {
        _mineViewModel = [SLMineViewModel shareInstance];
    }
    return _mineViewModel;
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
